package com.tjrac.generalpermissions.service.impl;

import com.tjrac.filter.stereotype.Param;
import com.tjrac.filter.stereotype.ParameterFilter;
import com.tjrac.filter.stereotype.PermissionControl;
import com.tjrac.generalpermissions.dao.UserDao;
import com.tjrac.generalpermissions.domain.User;
import com.tjrac.generalpermissions.epception.MsgException;
import com.tjrac.generalpermissions.epception.statusCode.SessionPermission;
import com.tjrac.generalpermissions.epception.statusCode.UserManagementCode;
import com.tjrac.generalpermissions.service.UserManagementService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ZeNing
 * @create 2020-12-01 15:46
 */
@Service
@Transactional
public class UserManagementServiceImpl implements UserManagementService {

    @Resource
    UserDao userDao;

    @PermissionControl("查询用户")
    @Override
    public User queryById(int id) throws MsgException {
        User user = userDao.queryById(id);
        if (user == null) {
            throw new MsgException(UserManagementCode.NOT_EXIST_USER, "用户不存在,查看失败");
        } else {
            return user;
        }
    }

    @PermissionControl("查询用户")
    @ParameterFilter(param = {@Param(index = 0, sessionKey = SessionPermission.QUERY_SINGLEUSER_RANGE)})
    @Override
    public User queryByUsername(String username) throws MsgException {
        User user = userDao.queryByUsername(username);
        if (user == null) {
            throw new MsgException(UserManagementCode.NOT_EXIST_USER, "用户不存在,查看失败");
        } else {
            return user;
        }
    }

    @PermissionControl("查询所有用户")
    @Override
    public String queryAllUser() throws MsgException {
        List<User> users = userDao.queryAll();
        if (users == null) {
            throw new MsgException(UserManagementCode.FAILURE_SELECT_ALL, "查找所有用户失败");
        } else {
            return new MsgException(UserManagementCode.SUCCESS_USER, "查找成功").toListJson(users);
        }
    }

    @PermissionControl("添加用户")
    @Override
    public String addUser(User user) throws MsgException {
        if (userDao.queryByUsername(user.getUsername()) != null) {
            throw new MsgException(UserManagementCode.EXIST_USER, "用户已存在,添加失败");
        } else {
            if (userDao.addUser(user) != 0) {
                return new MsgException(UserManagementCode.SUCCESS_USER, "添加用户成功").toJson();
            } else {
                throw new MsgException(UserManagementCode.FAILURE_ADD, "添加用户失败");
            }
        }
    }

    @PermissionControl("删除用户")
    @Override
    public String delUser(int id) throws MsgException {
        if (userDao.queryById(id) == null) {
            throw new MsgException(UserManagementCode.NOT_EXIST_USER, "用户不存在,删除失败");
        } else {
            if (userDao.delUser(id) != 0) {
                return new MsgException(UserManagementCode.SUCCESS_USER, "删除用户成功").toJson();
            } else {
                throw new MsgException(UserManagementCode.FAILURE_DEL, "删除用户失败");
            }
        }
    }

    @PermissionControl("修改用户")
    @Override
    public String modifyUser(User user) throws MsgException {
        if (userDao.queryById(user.getId()) == null) {
            throw new MsgException(UserManagementCode.NOT_EXIST_USER, "用户不存在,修改失败");
        } else {
            if (userDao.modifyById(user) != 0) {
                return new MsgException(UserManagementCode.SUCCESS_USER, "修改用户成功").toJson();
            } else {
                throw new MsgException(UserManagementCode.FAILURE_UPD, "修改用户成功");
            }
        }
    }

}
